C# Reading and Writing a Char[] to and from a Byte[]
Posted
by Simon G
on Stack Overflow
See other posts from Stack Overflow
or by Simon G
Published on 2010-03-29T09:02:08Z
Indexed on
2010/03/29
9:03 UTC
Read the original article
Hit count: 333
Hi,
I have a byte array of around 10,000 bytes which is basically a blob from delphi that contains char, string, double and arrays of various types. This need to be read in and updated via C#.
I've created a very basic reader that gets the byte array from the db and converts the bytes to the relevant object type when accessing the property which works fine. My problem is when I try to write to a specific char[] item, it doesn't seem to update the byte array. I've created the following extensions for reading and writing:
public static class CharExtension
{
public static byte ToByte( this char c )
{
return Convert.ToByte( c );
}
public static byte ToByte( this char c, int position, byte[] blob )
{
byte b = c.ToByte();
blob[position] = b;
return b;
}
}
public static class CharArrayExtension
{
public static byte[] ToByteArray( this char[] c )
{
byte[] b = new byte[c.Length];
for ( int i = 1; i < c.Length; i++ )
{
b[i] = c[i].ToByte();
}
return b;
}
public static byte[] ToByteArray( this char[] c, int positon, int length, byte[] blob )
{
byte[] b = c.ToByteArray();
Array.Copy( b, 0, blob, positon, length );
return b;
}
}
public static class ByteExtension
{
public static char ToChar( this byte[] b, int position )
{
return Convert.ToChar( b[position] );
}
}
public static class ByteArrayExtension
{
public static char[] ToCharArray( this byte[] b, int position, int length )
{
char[] c = new char[length];
for ( int i = 0; i < length; i++ )
{
c[i] = b.ToChar( position );
position += 1;
}
return c;
}
}
to read and write chars and char arrays my code looks like:
Byte[] _Blob; // set from a db field
public char ubin
{
get { return _tariffBlob.ToChar( 14 ); }
set { value.ToByte( 14, _Blob ); }
}
public char[] usercaplas
{
get { return _tariffBlob.ToCharArray( 2035, 10 ); }
set { value.ToByteArray( 2035, 10, _Blob ); }
}
So to write to the objects I can do:
ubin = 'C'; // this will update the byte[]
usercaplas = new char[10] { 'A', 'B', etc. }; // this will update the byte[]
usercaplas[3] = 'C'; // this does not update the byte[]
I know the reason is that the setter property is not being called but I want to know is there a way around this using code similar to what I already have?
I know a possible solution is to use a private variable called _usercaplas that I set and update as needed however as the byte array is nearly 10,000 bytes in length the class is already long and I would like a simpler approach as to reduce the overall code length and complexity.
Thank
© Stack Overflow or respective owner